// ==UserScript==
// @name         sz91160刷放号
// @namespace    http://www.91160.com/
// @version      2024-08-30
// @description  sz91160刷放号,仅用于测试和学习研究，禁止用于商业用途!!!
// @author       goson
// @match        *://*.91160.com/doctors/*
// @icon         
// @grant        none
// ==/UserScript==

(function() {
    'use strict';

    function GetDateStr(AddDayCount) {

        var dd = new Date();

        dd.setDate(dd.getDate()+AddDayCount);//获取AddDayCount天后的日期

        var y = dd.getFullYear();

        var m = (dd.getMonth()+1)<10?"0"+(dd.getMonth()+1):(dd.getMonth()+1);//获取当前月份的日期，不足10补0

        var d = dd.getDate()<10?"0"+dd.getDate():dd.getDate();//获取当前几号，不足10补0

        return y+"-"+m+"-"+d;

    }
    var fast_cnt = 4;  // 快刷次数
    var loop_cnt = 4; //  停顿秒数


    var audio = new Audio('https://downsc.chinaz.net/Files/DownLoad/sound1/201709/9239.mp3');

    function checkData(data){
        var turl = 'https://www.91160.com/guahao/ystep1/uid-21/depid-4383/schid-';
        var synum = 0;
        var tnum = 0;
        var sid = '';
        if(data){
            if(data.sch){
                if(Object.keys(data.sch).length>1){
                    var full_rst = '';
                    var fullflag = 0;
                    $.each(data.sch, function(index, value) {
                        console.log('索引：' + index + ', 值：' + value);
                        if (index.indexOf("4383_") !== -1) {
                            var robj = data.sch[index] ;
                            $.each(robj, function(index2, value2) {
                                var robj2 = robj[index2];
                                console.log('索引2：' + index2 + ', 值2：' + value2);

                                $.each(robj2, function(index3, value3) {

                                    var robj3 = robj[index3];
                                    console.log('索引3：' + index3 + ', 值3：' + value3);
                                    if(value3.y_state=='1'){
                                        turl = turl + value3.schedule_id + '.html';
                                        console.log('链接url= ' + turl )
                                        window.open(turl, '_blank');
                                        synum = value3.yuyue_num;
                                        tnum = value3.yuyue_max;
                                        sid = value3.schedule_id;
                                        document.getElementById('rst-lab').innerText = (value3.to_date + value3.time_type_desc + '剩余(' +  ',已约:' + value3.yuyue_num + ',总计:' + value3.yuyue_max + ')');
                                        audio.play();

                                        clearInterval(lschId);
                                        clearTimeout(lschId);
                                        clearTimeout(waitLid);
                                    }else{
                                        full_rst += ('<br/>'+ value3.to_date + value3.time_type_desc + '(' +  ',已约:' + value3.yuyue_num + ',总计:' + value3.yuyue_max + ')' );
                                        fullflag = 1;
                                    }
                                });
                            });
                        }
                    });
                    if(fullflag!=0){
                        console.log(full_rst)
                        document.getElementById('rst-lab').innerHTML  = full_rst +'<br/>'
                    }
                }
            }
        }
    }

    var v_unit_id
    if(typeof _unit_id === "undefined"){
        v_unit_id= 21;
    }else{
        v_unit_id= _unit_id;
    }

    var v_dep_id
    if(typeof _dep_id === "undefined"){
        v_dep_id= 4383;
    }else{
        v_dep_id= _dep_id;
    }

    var _user_key1 = '';
    if(typeof _user_key  === "undefined"){

    }else{
        _user_key1 = _user_key;
    }

    var qdate = GetDateStr(-4);
    var qurl= 'https://gate.91160.com/guahao/v1/pc/sch/doctor?user_key=' +_user_key1 + '&docid=' +docid + '&doc_id=' +docid + '&unit_id=' + v_unit_id + '&dep_id=' +v_dep_id + '&date=' +qdate + '&days=6';

    $.get(qurl, function(data) {
        console.log(data);
        if(data.sch){
            checkData(data);
        }
    }).fail(function(jqXHR, textStatus, errorThrown) {
        errStop();
        console.error('获取数据失败: ' + textStatus);
        console.error('错误详情: ' + errorThrown);
    },'json');


    var loopCnting = 0;
    function doGetAction(){
        lschId = setInterval(doGetActionIng,document.getElementById('loop-num').value);
    }

    function doGetActionIng(){
        if(loopCnting < document.getElementById('fmaxNum').value){
            loopCnting = loopCnting + 1;
            document.getElementById('loop-cnt-lab').innerText =  '-执行次数:'+ loopCnting;
            $.get(qurl, function(data) {
                console.log(data);
                if(data.sch){
                    checkData(data);
                }
            }).fail(function(jqXHR, textStatus, errorThrown) {
                errStop();
                console.error('获取数据失败: ' + textStatus);
                console.error('错误详情: ' + errorThrown);
            },'json');

            if(loopCnting%2 > 0){
               waitSts = 0;
                document.getElementById('loop-cnt-lab').style.backgroundColor = 'yellow';
            }else{
               waitSts = 1;
                document.getElementById('loop-cnt-lab').style.backgroundColor = 'pink';
           }
        }else{
            clearInterval(lschId);
            loopCnting=0;
            lschId = setTimeout(doGetAction,document.getElementById('jiangeTime').value);
            document.getElementById('loop-cnt-lab').innerText =  '-暂停中(秒)'+ document.getElementById('jiangeTime').value/1000;
            document.getElementById('loop-cnt-lab').style.backgroundColor = 'white';
        }
    }
    var waitLid = 0;
    var lschId =0;
    function createTopRightDiv() {
        deleteDivById('r-div');
        var div = document.createElement('div');
        div.id = 'r-div';
        div.style.position = 'fixed';
        div.style.top = '0';
        div.style.right = '0';
        div.style.width = '212px';
        div.style.zIndex = '9999';
        div.style.backgroundColor = 'white';
        div.style.padding = '10px';
        div.style.border = '1px solid #ddd';

        var bre = document.createElement("br");

        var labelx1 = document.createElement('label');
        labelx1.id = 'lnum'
        labelx1.textContent = ' 循环时间ms:';
        labelx1.style.marginBottom = '10px';

        var input1 = document.createElement('input');
        input1.type = 'text';
        input1.id ='loop-num';
        input1.name ='loop-num';
        input1.value = '1000';
        input1.placeholder = '循环时间ms';
        input1.style.marginBottom = '10px';
        input1.style.width = '118px';

        var labelx2 = document.createElement('label');
        labelx2.id = 'fnum'
        labelx2.textContent = '  快 查 次 数:  ';
        labelx2.style.marginBottom = '10px';

        var input2 = document.createElement('input');
        input2.type = 'text';
        input2.id ='fmaxNum';
        input2.name ='fmaxNum';
        input2.value = '4';
        input2.placeholder = '快速次数';
        input2.style.marginBottom = '10px';
        input2.style.width = '118px';

        var labelx3 = document.createElement('label');
        labelx3.id = 'jt'
        labelx3.textContent = ' 停顿时间ms:';
        labelx3.style.marginBottom = '10px';

        var input3 = document.createElement('input');
        input3.type = 'text';
        input3.id ='jiangeTime';
        input3.name ='jiangeTime';
        input3.value = '4000';
        input3.placeholder = '停顿时间ms';
        input3.style.marginBottom = '10px';
        input3.style.width = '118px';

        var labelx4 = document.createElement('label');
        labelx4.id = 'jt'
        labelx4.textContent = ' 开 始 时 间: ';
        labelx4.style.marginBottom = '10px';

        var input4 = document.createElement('input');
        input4.type = 'text';
        input4.id ='runTime';
        input4.name ='runTime';
        input4.value = '16:59:58';
        input4.placeholder = '开始时间';
        input4.style.marginBottom = '10px';
        input4.style.width = '118px';

        var label2 = document.createElement('label');
        label2.id = 'rst-lab'
        label2.textContent = ' - ';
        label2.style.marginBottom = '10px';
        label2.style.backgroundColor  = '#AEDaff';  // 背景颜色

        var labelx0 = document.createElement('label');
        labelx0.id = 'loop-cnt-lab'
        labelx0.textContent = ' 停止 ';
        labelx0.style.marginBottom = '10px';

        var button = document.createElement('button');
        button.id="actbtn";
        button.style.marginLeft = '10px';
        button.style.borderStyle = 'solid'; // 边框样式
        button.style.borderWidth = '1px';   // 边框宽度
        button.style.borderColor = 'red';  // 边框颜色
        button.style.backgroundColor  = '#87ff2a';  // 背景颜色
        button.innerText = '启/停';
        button.onclick = function() {
            if(lschId==0){
                console.log('循环间隔ms: ' + input1.value + ',执行');
                doGetAction();
                document.getElementById('actbtn').value = '已启动';
                document.getElementById('actbtn').innerText = '已启动';
                document.getElementById('rst-lab').innerText = '';
            }else{
                clearInterval(lschId);
                clearTimeout(lschId);
                clearTimeout(waitLid);
                document.getElementById('actbtn').value = '已停止';
                document.getElementById('actbtn').innerText = '已停止';
                document.getElementById('loop-cnt-lab').innerText =  '-';
                lschId = 0;
            }
        };

        var button2 = document.createElement('button');
        button2.id="autoactbtn";
        button2.style.marginLeft = '10px';
        button2.style.borderWidth = '1px';   // 边框宽度
        button2.style.borderColor = 'red';  // 边框颜色
        button2.style.backgroundColor  = '#87ff2a';  // 边框颜色
        button2.innerText = '自启 ';
        button2.onclick = function() {
            autoRun();
        };


        div.appendChild(labelx1);
        div.appendChild(input1);
        div.appendChild(bre);
        div.appendChild(labelx2);
        div.appendChild(input2);
        div.appendChild(bre);
        div.appendChild(labelx3);
        div.appendChild(input3);
        div.appendChild(labelx4);
        div.appendChild(input4);
        div.appendChild(bre);
        div.appendChild(button2);
        div.appendChild(bre);
        div.appendChild(button);
        div.appendChild(labelx0);
        div.appendChild(bre);
        div.appendChild(label2);
        // 将div添加到body
        document.body.appendChild(div);

        console.log('fmaxNum='+document.getElementById('fmaxNum').value);
        console.log('jiangeTime='+document.getElementById('jiangeTime').value);
    }

    createTopRightDiv();

    function deleteDivById(id) {
        var div = document.getElementById(id);
        if (div) {
            div.parentNode.removeChild(div);
        }
    }

    function errStop(){
        clearInterval(lschId);
        clearTimeout(lschId);
        document.getElementById('actbtn').value = '已停止';
        document.getElementById('actbtn').innerText = '已停止';
        lschId = setTimeout(doGetAction,(document.getElementById('jiangeTime').value*2));
        document.getElementById('loop-cnt-lab').innerText =  '-暂被黑名单中暂停(秒)'+ (document.getElementById('jiangeTime').value*2)/1000;
    }

    var waitSts = 0;
    function autoRun(){
        var srun_time = document.getElementById('runTime').value;
        const currentDate = new Date();
        var vyear = currentDate.getFullYear(); // 获取当前年份，例如：2022
        var vmonth = currentDate.getMonth() + 1; // 获取当前月份，需要加1，因为月份从0开始，例如：7
        var vday = currentDate.getDate(); // 获取当前日期，例如：6

        let ntime1=new Date().getTime();
        let ntime2=new Date(vyear+'-'+vmonth+'-'+vday+' '+srun_time).getTime()
        console.log('当前时间='+ntime1);
        console.log('设定时间='+ntime2);
        if(ntime2<ntime1){
            console.log('检查时间到');
            {
                console.log('循环间隔ms: ' + document.getElementById('loop-num').value + ',执行');
                doGetAction();
                document.getElementById('actbtn').value = '已启动';
                document.getElementById('actbtn').innerText = '已启动';
                document.getElementById('rst-lab').innerText = '';
            }
        }else{
            console.log('时间未到，等待中');
            waitLid = setTimeout(autoRun,500);
            document.getElementById('loop-cnt-lab').innerText =  '-停止,等待启动中'
            if(waitSts > 0){
               waitSts = 0;
                document.getElementById('loop-cnt-lab').style.backgroundColor = 'yellow';
            }else{
               waitSts = 1;
                document.getElementById('loop-cnt-lab').style.backgroundColor = 'white';
           }
        }
    }

})();